class Torque{

  static sum(torques){

    const mass = torques
      .map(t => t.mass)
      .reduce((acc, v) => acc + v, 0);

    const center = torques
      .map(t => t.toProduct())
      .reduce((sum, vec) => sum.plus(vec), new Vec())
      .mult((torques.length === 0) ? 0 : 1/mass)

    return new Torque({
      center,
      mass
    })    
  }

  static fromVec(vec){
    return new Torque({center:(new Vec()).lerp(vec, 0.5), mass: vec.mag()});
  }

  static fromVecs(head, tail) {
    return new Torque({center:tail.plus(head).mult(0.5), mass: tail.dist(head)});
  }
  
  constructor({center=new Vec(), mass=0}={}){
    this.center = center;
    this.mass   = mass;
  }

  toProduct(){
    const {center, mass} = this;
    const copy = center.copy();
    copy.mult(mass);
    return copy;
  }
}
